package paywithdrawlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/withdraw"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type WithdrawLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	tx *gorm.DB
}

func NewWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawLogic {
	return &WithdrawLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// ------------------c端用户提现页面---------------------------------------------------------
func (l *WithdrawLogic) Withdraw(in *rpc.UserWithDrawReq) (ret *rpc.UserWithDrawRes, err error) {
	if in.Amount < 0 {
		return nil, xerrors.ErrInvalidParameterAmount
	}
	if in.WithdrawUserChannelId == 0 {
		return nil, xerrors.ErrUserChannelNot
	}
	m2 := withdraw.NewTypeModel(l.ctx, l.svcCtx)
	err = m2.WithdrawOpen(in.WithdrawTypeId)
	if err != nil {
		return nil, err
	}
	m := withdraw.NewWithdrawModel(l.ctx, l.svcCtx)
	return m.Withdraw(in)
}
